package api

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-playground/validator/v10"
)

var _validator = validator.New()

func init() {
	_validator.SetTagName("vd")
}

func BindRequest(req any, g *gin.Context, binds ...binding.Binding) error {
	if err := g.ShouldBind(req); err != nil {
		return err
	}

	for _, bind := range binds {
		if err := bind.Bind(g.Request, req); err != nil {
			return err
		}
	}
	return nil
}
func BindRequestG[R any](g *gin.Context, binds ...binding.Binding) (*R, error) {
	res := new(R)
	return res, BindRequest(res, g, binds...)
}

func VerifyRequest(r any) error {
	return _validator.Struct(r)
}
